import {defineStore} from 'pinia';
import workApi from "@/http/workApi";

export const useSuperAdminStore = defineStore('superAdmin', {
    actions: {
        setQuestion() {
            workApi.get('super_admin/question').then((res) => {
                this.question = res.data
            })
        },
        updateTaskQuestionBank() {
            workApi.get('super_admin/project').then((res) => {
                this.taskQuestionBank = res.data
            })
        },
        updateDataSource() {
            workApi.get('super_admin/get_data_source').then((res) => {
                this.dataSource.enable = res.data
            })
        },
    },


    state: () => ({
        question: [],
        taskQuestionBank: {
            disable: [],
            enable: []
        },
        dataSource: {
            disable: [],
            enable: []
        }
    }),

    getters: {
        getDataSourceTree() {
            const dataSourceTypeSongs = {
                file: '文件',
                api: 'API',
                web: '网页',
                database: '数据库',

            }
            const a = (array, key) => {
                for (let item in array) {
                    if (key === array[item].value) {
                        return item
                    }
                }
                return false
            }
            const tree = []
            for (let i of this.dataSource.enable) {
                let itemI = a(tree, i.data_source_type)
                if (!itemI) {
                    tree.push({
                        label: dataSourceTypeSongs[i.data_source_type],
                        value: i.data_source_type,
                        children: []
                    });
                    itemI = tree.length - 1
                }
                let itemJ = a(tree[itemI].children, i.data_source_type+"-"+i.category)
                if (!itemJ) {
                    tree[itemI].children.push({
                        label: i.category,
                        value: i.data_source_type + "-" + i.category,
                        children: []
                    })
                    itemJ = tree[itemI].children.length - 1
                }
                tree[itemI].children[itemJ].children.push({
                    label: i.name,
                    value: i.id
                })
            }
            console.log(tree)
            return tree
        },
    },
})
